#include "DXUT.h"
#include "PostProcessEffectFile.h"

#include "SDKmisc.h"
 
CPostProcessEffectFile::CPostProcessEffectFile( const TCHAR * resName,const TCHAR * szFilePath, D3DPOOL pool /*= D3DPOOL_DEFAULT*/ )
:CGraphicsFileResource(RESOURCE_TYPE_POSTEFFECT,resName,szFilePath,pool) ,m_hTechnique(0)
	 
{

}


CPostProcessEffectFile::~CPostProcessEffectFile(void)
{
}

void CPostProcessEffectFile::OnD3D9DestroyDevice()
{
	SAFE_RELEASE(m_pEffect);
}

HRESULT CPostProcessEffectFile::OnD3D9ResetDevice( IDirect3DDevice9* pd3dDevice )
{
return	m_pEffect->OnResetDevice();
}

void CPostProcessEffectFile::OnD3D9LostDevice()
{
	m_pEffect->OnLostDevice();
}

HRESULT CPostProcessEffectFile::OnD3D9CreateDevice( IDirect3DDevice9* pd3dDevice )
{

	HRESULT hr;
	WCHAR wszPath[MAX_PATH];

	if( FAILED( hr = DXUTFindDXSDKMediaFileCch( wszPath, MAX_PATH, this->GetFileName() ) ) )
		return hr;
 
	LPD3DXBUFFER  pError;
	hr = D3DXCreateEffectFromFile( pd3dDevice,
		wszPath,
		NULL,
		NULL,
		0,
		0,
		&m_pEffect,
		&pError );

	if( FAILED(hr))
	{
	 
		MessageBoxA(0,(char*)pError->GetBufferPointer(),0,0);
		pError->Release();
		return hr;
			 
	}


	hr = m_pEffect->FindNextValidTechnique(NULL, &m_hTechnique);
	if( FAILED( hr ) )
	{
		DXTRACE_ERR_MSGBOX(DXGetErrorString(hr),hr);
		SAFE_RELEASE(m_pEffect);
		return false;
	}


	hr = m_pEffect->GetTechniqueDesc(m_hTechnique, &m_techniqueDesc);
	if( FAILED( hr ) )
	{

		DXTRACE_ERR_MSGBOX(DXGetErrorString(hr),hr);
		SAFE_RELEASE(m_pEffect);
		return false;
	}

	hr = m_pEffect->SetTechnique(m_hTechnique);
	if( FAILED( hr ) )
	{
		DXTRACE_ERR_MSGBOX(DXGetErrorString(hr),hr);
		SAFE_RELEASE(m_pEffect);
		return false;
	}
	assert(NULL!=(m_hSrcTexture=m_pEffect->GetParameterByName(0, ("g_txSrcColor"))));
	return  hr;

}




bool CPostProcessEffectFile::begin()
{

	HRESULT result = m_pEffect->Begin(0, D3DXFX_DONOTSAVESTATE|D3DXFX_DONOTSAVESHADERSTATE);
	if( FAILED( result ) )
	{
		DXTRACE_ERR_MSGBOX(DXGetErrorString(result),result);

		return false;
	}

	return true;
}

bool CPostProcessEffectFile::activatePass(int pass)
{
	HRESULT result = m_pEffect->BeginPass(pass);
	if( FAILED( result ) )
	{
		DXTRACE_ERR_MSGBOX(DXGetErrorString(result),result);
		return false;
	}
	return true;
}

bool CPostProcessEffectFile::CommitChanges()
{
	HRESULT result = m_pEffect->CommitChanges();
	if( FAILED( result ) )
	{
		DXTRACE_ERR_MSGBOX(DXGetErrorString(result),result);
		return false;
	}
	return true;
}

bool CPostProcessEffectFile::endPass()
{
	HRESULT result = m_pEffect->EndPass();
	if( FAILED( result ) )
	{
		DXTRACE_ERR_MSGBOX(DXGetErrorString(result),result);
		return false;
	}
	return true;
}

void CPostProcessEffectFile::end()
{

	assert ( m_pEffect);
	HRESULT result = m_pEffect->End();
	if( FAILED( result ) )
	{
		DXTRACE_ERR_MSGBOX(DXGetErrorString(result),result);
	}
}

int CPostProcessEffectFile::totalPasses() const
{
	return m_techniqueDesc.Passes;
}
